package com.juyu.miliao.authentication.security;

import com.juyu.miliao.authentication.service.UserService;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collections;

public class DefaultAuthenticationProvider implements AuthenticationProvider {
    private final UserService userService;

    public DefaultAuthenticationProvider(UserService userService) {
        if (userService == null)
            throw new NullPointerException();
        this.userService = userService;
    }

    @Override
    public Authentication authenticate(Authentication token) throws AuthenticationException {
        if (UserNamePassWordRegisterToken.class.equals(token.getClass()))
            return authenticate((UserNamePassWordRegisterToken) token);
        if (PhoneNumberRegisterToken.class.equals(token.getClass()))
            return authenticate((PhoneNumberRegisterToken) token);
        if (ThirdPartyLoginToken.class.equals(token.getClass()))
            return authenticate((ThirdPartyLoginToken) token);
        if (AutomaticLogonRegisterToken.class.equals(token.getClass()))
            return authenticate((AutomaticLogonRegisterToken) token);
        if (UserNameVerificationCodeToken.class.equals(token.getClass()))
            return authenticate((UserNameVerificationCodeToken) token);
        return null;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        if (UserNamePassWordRegisterToken.class.equals(clazz))
            return true;
        if (PhoneNumberRegisterToken.class.equals(clazz))
            return true;
        if (ThirdPartyLoginToken.class.equals(clazz))
            return true;
        if (AutomaticLogonRegisterToken.class.equals(clazz))
            return true;
        if (UserNameVerificationCodeToken.class.equals(clazz))
            return true;
        return false;
    }

    //用户名密码登录
    private UsernamePasswordAuthenticationToken authenticate(UserNamePassWordRegisterToken token) throws AuthenticationException {
        UserDetails user = userService.loadUserByUsername(token.getName(), token.getCredentials().toString(),token.getCommonHead());
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
        return auth;
    }
    //用户名验证码登录
    private UsernamePasswordAuthenticationToken authenticate(UserNameVerificationCodeToken token) throws AuthenticationException {
        UserDetails user = userService.loadUserByVerificationCode(token.getName(),token.getIp(), token.getCredentials().toString(),token.getCommonHead());
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
        return auth;
    }

    //一键登录
    private UsernamePasswordAuthenticationToken authenticate(AutomaticLogonRegisterToken token) throws AuthenticationException {
        UserDetails userDetails = userService.automaticLogon(token.getDeviceNo(), token.getIp(), token.getCommonHead());
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
        return auth;
    }

    //手机号注册
    private UsernamePasswordAuthenticationToken authenticate(PhoneNumberRegisterToken token) throws AuthenticationException {
        UserDetails userDetails = new User(token.getName(), (String) token.getCredentials(), Collections.emptyList());
        userDetails = userService.registerAndLogin(userDetails, token.getNickName(), token.getSex(), token.getIcon(), token.getIp(), token.getCommonHead());
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
        return auth;
    }

    //第三方登录注册
    private UsernamePasswordAuthenticationToken authenticate(ThirdPartyLoginToken token) throws AuthenticationException {
        UserDetails userDetails = new User(token.getName(), (String) token.getCredentials(), Collections.emptyList());
        if (token.getType() == 1) {
            userDetails = userService.qqLogin(userDetails, token.getAccessToken(), token.getIp(), token.getCommonHead());
        } else
            userDetails = userService.wxLogin(userDetails, token.getAccessToken(), token.getIp(), token.getCommonHead());
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
        return auth;
    }

}
